package net.zhengxinyun.performance.service.indicators;

import net.zhengxinyun.performance.entity.SixIndicatorsEntity;
import net.zhengxinyun.performance.entity.SixTownEntity;
import net.zhengxinyun.performance.filter.SixTownFilter;
import net.zhengxinyun.performance.mapper.Indicators.SixTownEntityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * Created by hyp1202 on 2018/11/27.
 */
@Service
public class SixTownServiceImpl  implements SixTownService{

    private SixTownEntityMapper sixTownEntityMapper;

    @Autowired
    public SixTownServiceImpl(SixTownEntityMapper sixTownEntityMapper) {
        this.sixTownEntityMapper = sixTownEntityMapper;
    }

    @Override
    public int delete(SixTownEntity sixTownEntity) {
        return sixTownEntityMapper.deleteByPrimaryKey(sixTownEntity.getId());
    }

    @Override
    public int update(SixTownEntity sixTownEntity) {
        return sixTownEntityMapper.updateByPrimaryKey(sixTownEntity);
    }

    @Override
    public String insert(SixIndicatorsEntity townIndicatorsEntity, List<SixTownEntity> sixList) {
        Long lastId = townIndicatorsEntity.getId();
        for (SixTownEntity sixTownEntity : sixList) {
            sixTownEntity.setIndexId(lastId.toString());
            long a = sixTownEntityMapper.insert(sixTownEntity);
            if (a != 1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return "添加失败,请联系管理员";
            }
        }
        return "添加成功";
    }

    @Override
    public List<SixTownFilter> query(SixTownFilter townFilter) {
        return sixTownEntityMapper.query(townFilter);
    }
}
